<?php

namespace Addons\OaSystem\Controller;
use Addons\OaSystem\Controller\OaDutyBaseController;

class OaDutyController extends OaDutyBaseController
{
    protected $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_duty');
        parent::_initialize();

    }
    function lists(){
        $list_data = $this->_get_model_list( $this->model );

        $grids = $list_data['list_grids'];
        $data = db_select('oa_duty', null, 'duty_no asc');
        foreach ($data as $i => $v) {
            $editUrl = U('edit', array('id'=> $v['id']));
            $delUrl = U('del', array('id'=> $v['id']));
            $data[$i]['urls'] = "<a href=$editUrl>编辑</a>".'   '."<a class='confirm' href=$delUrl>删除</a>";
        }

        $this -> assign('list_grids', $grids);
        $this -> assign('list_data', $data);
        $this->display();
    }

    function add(){

        if(IS_POST) {
           $data['appid'] = $this->appId;
           $data['duty_no'] = $_POST['duty_no'];
           $data['name'] = $_POST['name'];
           $data['remark'] = $_POST['remark'];
           $data['level'] = $_POST['level'];
           $data['is_del'] = 0;

            if(db_find('oa_duty', ['name'=>$data['name']])) {
               $this->error('该职务名称已经存在, 请重新输入！');
               return false;
           }

            if(db_find('oa_duty', ['duty_no'=>$data['duty_no']])) {
                $this->error('该职务编号已经存在， 请重新输入！');
                return false;
            }

            M('oa_duty')->add($data);

            $this->success ( '创建' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
        }
        else {
            $this->display('add');
        }

    }
}